JavaScript syntax
part 31/43 Β· 161.3 KB total
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
properties of array object, if any. Thus it may be better to use a
traditional for loop with a numeric index when iterating over arrays.
β’ There are differences between the various Web browsers with regard to
which properties will be reflected with the for...in loop statement. In
theory, this is controlled by an internal state property defined by the
ECMAscript standard called "DontEnum", but in practice, each browser
returns a slightly different set of properties during introspection. It
is useful to test for a given property using if
(some_object.hasOwnProperty(property_name)) { ... }. Thus, adding a
method to the array prototype with Array.prototype.newMethod =
function() {...} may cause for ... in loops to loop over the method's
name.
While loop
The syntax of the JavaScript while loop is as follows:
while (condition) {
statement1;
statement2;
statement3;
...
}
Do ... while loop
The syntax of the JavaScript do ... while loop is as follows:
do {
statement1;
statement2;
statement3;
...
} while (condition);
With
The with statement adds all of the given object's properties and methods
into the following block's scope, letting them be referenced as if they
were local variables.
with (document) {
const a = getElementById('a');
const b = getElementById('b');
const c = getElementById('c');
};
β’ Note the absence of document. before each getElementById() invocation.
The semantics are similar to the with statement of Pascal.
Because the availability of with statements hinders program performance
and is believed to reduce code clarity (since any given variable could
actually be a property from an enclosing with), this statement is not
allowed in strict mode.
Labels
JavaScript supports nested labels in most implementations. Loops or
blocks can be labeled for the break statement, and loops for continue.
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ